home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / CharenderView.BackModule / CharenderView.m < prev    next >
Text File  |  1995-06-12  |  6KB  |  261 lines

  1. /***************************************************
  2. **
  3. **    CharenderView.m
  4. **
  5. **    By Brian Hobbs (brian@ny.shl.com)
  6. **
  7. **    This is the main class of the bundle, sets
  8. **    up the defaults, environment, text string, and
  9. **    stuff.  Everything but the code in the initFrame:
  10. **    method should be quite unsurprising.
  11. **
  12. **    This code is public domain.  Feel free to use
  13. **    it in any way you wish.  I'd like to see any 
  14. **    changes you make, tho.
  15. **
  16. **    Kudos to Sam Streeper for the Teapot code, from
  17. **    which most of the BackSpace 3-D setup was "acquired".
  18. **
  19. ****************************************************/
  20.  
  21. #import "CharenderView.h"
  22.  
  23. #define CHARENDERTEXTDEFAULT    "CharenderText"
  24. #define CHARENDERFONTDEFAULT    "CharenderFont"
  25. #define CHARENDERCOLORDEFAULT    "CharenderColor"
  26. #define CHARENDERSURFDEFAULT    "CharenderSurf"
  27. #define CHARENDERRESDEFAULT        "CharenderRes"
  28.  
  29. @implementation CharenderView
  30.  
  31. + initialize
  32. {
  33.   static NXDefaultsVector CharenderDefaults={
  34.     {CHARENDERTEXTDEFAULT,"Darkninja"},
  35.     {CHARENDERFONTDEFAULT,"Helvetica"},
  36.     {CHARENDERCOLORDEFAULT,"1.0 1.0 1.0"},
  37.     {CHARENDERSURFDEFAULT,"2"},
  38.     {CHARENDERRESDEFAULT,"30"},
  39.     {NULL}};
  40.   NXRegisterDefaults([NXApp appName], CharenderDefaults);
  41.   return [super initialize];
  42. }
  43.  
  44. - initFrame:(const NXRect *) theRect
  45. {    
  46.     // camera position points
  47.     RtPoint fromP = {0,0,-5.0}, toP = {0,0,0};
  48.  
  49.     // light position points
  50.     RtPoint lFromP = {30,20,-20};
  51.     RtPoint lFromP2 = {-10,10,-10};
  52.     RtPoint lFromP3 = {0,-10,-10};
  53.  
  54.     // the various 3Dkit object id''s that we will initialize here
  55.     id ambientLight;
  56.     id aLight;
  57.  
  58.     [super initFrame:theRect];
  59.  
  60.     // retrieve defaults
  61.     [self getDefaults];
  62.  
  63.     [self inspector:(BSThinker())];
  64.     
  65.     [self setEyeAt:fromP toward:toP roll:0.0];
  66.         
  67.     theShader=[[N3DShader alloc] init];
  68.     [theShader setUseColor:YES];
  69.     [theShader setColor:colorDefault];
  70.     [(N3DShader *)theShader setShader:"plastic"];
  71.  
  72.     textShape= (DN3DString *)[[DN3DString alloc] init];
  73.     [textShape setTextFont:fontDefault];
  74.     [textShape setTextString:textDefault];
  75.     textBox = [[N3DShape alloc] init];
  76.       
  77.     [textShape setShader:theShader];
  78. //    [[self setWorldShape:textShape] free];
  79.  
  80.     [[self worldShape] linkDescendant:textBox];
  81.     [textBox linkDescendant:textShape];
  82.  
  83.     ambientLight=[[N3DLight alloc] init];
  84.     [ambientLight makeAmbientWithIntensity:0.1];
  85.     [self addLight:ambientLight];
  86.     
  87.     aLight=[[N3DLight alloc] init];
  88.     [aLight makeDistantFrom:lFromP to:toP intensity:0.2];
  89.     [self addLight:aLight];
  90.  
  91.     aLight=[[N3DLight alloc] init];
  92.     [aLight makeDistantFrom:lFromP2 to:toP intensity:0.9];
  93.     [self addLight:aLight];
  94.  
  95.     aLight=[[N3DLight alloc] init];
  96.     [aLight makeDistantFrom:lFromP3 to:toP intensity:0.3];
  97.     [self addLight:aLight];
  98.  
  99.     [self setSurfaceTypeForAll:surfaceTypeDefault chooseHider:YES];
  100.  
  101.     dx = randBetween(.05, .2);
  102.     dy = randBetween(.05, .2);
  103.     dz = randBetween(.05, .2);
  104.     rotationAxis[0] = 0;
  105.     theta = 0;
  106.  
  107.     return self;
  108. }
  109.     
  110. #define PI 3.14159
  111.  
  112. - oneStep
  113. {
  114.     RtMatrix m;
  115.  
  116.     rotationAxis[1] = sin(theta);
  117.     rotationAxis[2] = cos(theta);
  118.     theta += (2.0 * PI / 180.0);
  119.  
  120.     [textShape rotateAngle:5 axis:rotationAxis];
  121.     [textBox translate:dx :dy :dz];
  122.  
  123.     [textBox getTransformMatrix:m];
  124.  
  125. // I can use these values directly only because string's box isn't rotated
  126.     if (m[3][0] < -1.5) dx = randBetween(.02, .1);
  127.     else if (m[3][0] > 1.5) dx = randBetween(-.02, -.1);
  128.     if (m[3][1] < -1.0) dy = randBetween(.02, .1);
  129.     else if (m[3][1] > 1.5) dy = randBetween(-.02, -.1);
  130.     if (m[3][2] < -2.25) dz = randBetween(.02, .1);
  131.     else if (m[3][2] > 4) dz = randBetween(-.02, -.1);
  132.  
  133.     [self display];
  134.  
  135.     return self;
  136. }
  137.  
  138. - (BOOL) useBufferedWindow
  139. {
  140.     return YES;
  141. }
  142.  
  143. - (const char *)windowTitle
  144. {    return "Charender";
  145. }
  146.  
  147. - inspector:sender
  148. {
  149.     char buf[MAXPATHLEN];
  150.     
  151.     if (!inspectorPanel)
  152.     {
  153.         sprintf(buf,"%s/Charender.nib",[sender  moduleDirectory:"Charender"]);
  154.         [NXApp loadNibFile:buf owner:self withNames:NO];
  155.  
  156.         [stringField setStringValue:textDefault];
  157.         [stringField setFont:[Font newFont:fontDefault size:12.0]];
  158.         [colorWell setColor:colorDefault];
  159.         [surfaceMatrix selectCellAt:surfaceTypeDefault :0];
  160.         [resolutionTextField setIntValue:resolutionDefault];
  161.         [resolutionSlider setIntValue:resolutionDefault];
  162.         
  163.         [inspectorPanel display];
  164.         NXPing();
  165.     }
  166.     return inspectorPanel;
  167. }
  168.  
  169. - setStringAndFontFrom:sender
  170. {
  171.     strcpy(textDefault, [sender stringValue]);
  172.     strcpy(fontDefault, [[sender font] name]);
  173.     
  174.     [textShape setTextString:textDefault andFont:fontDefault];
  175.     
  176.     NXWriteDefault([NXApp appName], CHARENDERTEXTDEFAULT, textDefault);
  177.     NXWriteDefault([NXApp appName], CHARENDERFONTDEFAULT, fontDefault);
  178.  
  179.     [self display];
  180.     
  181.     return self;
  182. }
  183.         
  184. - setColorFrom:sender
  185. {
  186.     float r,g,b;
  187.     char str[100];
  188.  
  189.     colorDefault = [sender color];
  190.     
  191.     [theShader setColor:colorDefault];
  192.  
  193.     NXConvertColorToRGB(colorDefault, &r, &g, &b);
  194.     sprintf(str, "%5.3f %5.3f %5.3f", r, g, b );
  195.     NXWriteDefault([NXApp appName], CHARENDERCOLORDEFAULT, str);
  196.  
  197.     [self display];
  198.  
  199.     return self;
  200. }
  201.  
  202. - setSurfaceTypeFrom:sender
  203. {
  204.     char str[100];
  205.  
  206.     surfaceTypeDefault = [sender selectedRow];
  207.     
  208.     sprintf(str,"%d", surfaceTypeDefault);
  209.     NXWriteDefault([NXApp appName], CHARENDERSURFDEFAULT, str);
  210.  
  211.     [self setSurfaceTypeForAll:surfaceTypeDefault chooseHider:YES];
  212.  
  213.     [self display];
  214.  
  215.     return self;
  216. }
  217.  
  218. - setResolutionFrom:sender
  219. {
  220.     char str[100];
  221.  
  222.     resolutionDefault = [sender intValue];
  223.  
  224.     sprintf(str,"%d", resolutionDefault);
  225.     NXWriteDefault([NXApp appName], CHARENDERRESDEFAULT, str);
  226.     
  227.     [textShape setResolution:(float)resolutionDefault];
  228.     [resolutionTextField setIntValue:resolutionDefault];
  229.     
  230.     [self display];
  231.     
  232.     return self;
  233. }
  234.  
  235. - getDefaults
  236. {
  237.     const char *ptr;
  238.     float r, g, b;
  239.     
  240.     textDefault = (char *)NXZoneMalloc([self zone], sizeof(char) * 80);
  241.     fontDefault = (char *)NXZoneMalloc([self zone], sizeof(char) * 80);
  242.  
  243.     ptr = NXGetDefaultValue([NXApp appName], CHARENDERTEXTDEFAULT);
  244.         strcpy(textDefault, (char *)ptr);
  245.  
  246.     ptr = NXGetDefaultValue([NXApp appName], CHARENDERFONTDEFAULT);
  247.         strcpy(fontDefault, (char *)ptr);
  248.  
  249.     ptr = NXGetDefaultValue([NXApp appName], CHARENDERCOLORDEFAULT);
  250.         sscanf (ptr, "%f %f %f", &r, &g, &b );
  251.         colorDefault = NXConvertRGBToColor(r,g,b);
  252.  
  253.     surfaceTypeDefault = atoi(NXGetDefaultValue([NXApp appName], CHARENDERSURFDEFAULT));
  254.     
  255.     resolutionDefault = atoi(NXGetDefaultValue([NXApp appName], CHARENDERRESDEFAULT));
  256.     
  257.     return self;
  258. }
  259.  
  260. @end
  261.